package com.loop.unclelend.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.loop.unclelend.MyApplication
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UnclePopupStepCommonBinding
import com.loop.unclelend.utils.adapter.UncleOnItemChildClickListener
import com.loop.unclelend.utils.adapter.UncleRecyclerViewAdapter
import com.loop.unclelend.utils.adapter.UncleViewHolderHelper
import com.loop.unclelend.utils.popup.EasyPopup
import com.loop.unclelend.utils.popup.XGravity
import com.loop.unclelend.utils.popup.YGravity
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/12 14:46
 * @Annotation：列表选择
 */
class Popup_Step_Common(val context: Context) {
    lateinit var binding: UnclePopupStepCommonBinding

    /* 筛选框宽度，默认屏幕宽度 */
    var popupWidth = MyApplication.app.getPhoneWidth()
    //选中的item
    var selectItem: JSONObject? = null

    private var mItemListener: IItemClickListener? = null

    interface IItemClickListener {
        fun onConfirmAction(data: JSONObject?)
        fun onDismissListener()
    }
    private var mEasyPopup: EasyPopup? = null

    //分类
    private var mAdapter: BrandAdapter? = null

    fun show(view: View, dimView: ViewGroup) {
        if (mEasyPopup == null) {
            showViewDown(view, dimView)
        } else {
            mEasyPopup!!.showAtAnchorView(view, YGravity.BELOW, XGravity.CENTER, 0, 0)
        }
    }

    /* 选择回调 */
    fun setSelectItemListener(listener: IItemClickListener?) {
        mItemListener = listener
    }

    /* 设置宽度 */
    fun setWindowWidth(width: Int){
        this.popupWidth = width
    }

    /* 设置分类数据 */
    fun setBrandList(list: List<JSONObject>) {
        this.mAdapter!!.data = list
    }

    private fun showViewDown(view: View, dimView: ViewGroup) {
        mEasyPopup = EasyPopup(context)
        binding = UnclePopupStepCommonBinding.inflate(LayoutInflater.from(context))
        mEasyPopup!!.setContentView(binding.root)
        mEasyPopup!!.width = this.popupWidth
//        mEasyPopup!!.height = Apps.getPhoneHeight() / 2
        mEasyPopup!!.setAnimationStyle(R.style.popup_anim)
        //是否允许点击PopupWindow之外的地方消失
        mEasyPopup!!.setFocusAndOutsideEnable(true)
        //允许背景变暗
        mEasyPopup!!.setBackgroundDimEnable(false)
        //变暗的透明度(0-1)，0为完全透明
        mEasyPopup!!.setDimValue(0.4f)
        //变暗的背景颜色
        mEasyPopup!!.setDimColor(Color.BLACK)
        //指定任意 ViewGroup 背景变暗
        mEasyPopup!!.setDimView(dimView)
//        mEasyPopup!!.apply();
        EasyPopup.create()
        mEasyPopup!!.showAtAnchorView(view, YGravity.BELOW, XGravity.CENTER, 0, 0)
        //排序列表
        initRecyclerView()
        mEasyPopup!!.setOnDismissListener { // popupWindow隐藏时恢复屏幕正常透明度
            if (mItemListener != null) {
                mItemListener!!.onDismissListener()
            }
        }
    }

    private fun initRecyclerView() {
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        this.mAdapter = BrandAdapter( binding.recyclerView)
        binding.recyclerView.adapter = this.mAdapter
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        this.mAdapter!!.setOnItemChildClickListener(onItemClickListener()) //点击
    }

    /* RecyclerView点击Item */
    internal inner class onItemClickListener : UncleOnItemChildClickListener {
        @SuppressLint("NotifyDataSetChanged")
        override fun onItemChildClick(parent: ViewGroup, childView: View, position: Int) {
            val model = mAdapter!!.getItem(position)
            if (childView.id == R.id.itemLayout) {
                for (brandItem in mAdapter!!.data) {
                    brandItem.put("isSelect",false)
                    if (brandItem!!.optString("title") == model.optString("title")) {
                        brandItem.put("isSelect",true)
                    }
                }
                mAdapter!!.notifyDataSetChanged()
                if (mItemListener != null) {
                    mItemListener!!.onConfirmAction(model)
                    mEasyPopup!!.dismiss()
                }
            }
        }
    }

    internal inner class BrandAdapter(recyclerView: RecyclerView?) : UncleRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.uncle_popup_step_common_item) {
        override fun setItemChildListener(helper: UncleViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }

        override fun fillData(helper: UncleViewHolderHelper, position: Int, model: JSONObject) {
            val textTitle = helper.getTextView(R.id.txt_title)
            textTitle.text = model.optString("title")

            val itemLayout = helper.getView<LinearLayout>(R.id.itemLayout)
            if (model.optBoolean("isSelect",false)) {
                helper.setVisibility(R.id.img_select,View.VISIBLE)
                itemLayout.setBackgroundColor(Color.parseColor("#307000FF"))
            } else {
                helper.setVisibility(R.id.img_select,View.INVISIBLE)
                itemLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white))
            }
            if (position == mData.size - 1){
                helper.setVisibility(R.id.v_line,View.INVISIBLE)
            } else {
                helper.setVisibility(R.id.v_line,View.INVISIBLE)
            }
        }
    }
}